aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/(profile)/+layout.svelte
blob: 6dacf2ce5a52173d648ce609eaa0ee1c10c0fd01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<script>
	import { f, s, sats } from '$lib/utils';
	import { animatedRate, selectedRate } from '$lib/store';
	import { Icon } from '$comp';
	import { t } from '$lib/translations';
	import { sign, send } from '$lib/nostr';
	import { v4 } from 'uuid';

	export let data;
	let events, user, subject, src, text;
	$: ({ events, user, subject, src, text } = data);
	$: ({ currency, username: n, display } = subject);

	$: username = n.length > 60 ? n.substr(0, 6) : display || n;

	let follow = async () => {
		user.follows.push(['p', subject.pubkey, 'wss://nostr.coinos.io', subject.username]);
		update();
	};

	let unfollow = async () => {
		user.follows.splice(
			user.follows.findIndex((t) => t[1] === subject.pubkey),
			1
		);
		update();
	};

	let update = async () => {
		let event = {
			pubkey: user.pubkey,
			created_at: Math.floor(Date.now() / 1000),
			kind: 3,
			content: '',
			tags: user.follows
		};

		await sign({ event, user });
		await send(event);

		user.follows = user.follows;
	};

	$: following = !!user?.follows.find((t) => t.includes(subject.pubkey));

	let ease = (t) => (t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t);
	let i;

	let o = $selectedRate;
	$: animate($selectedRate);
	let animate = (n) => {
		clearInterval(i);

		let t = 0;
		let d = o - n;

		i = setInterval(() => {
			$animatedRate = (o - d * ease(t / 100)).toFixed(2);
			if (t > 80) (o = n) && clearInterval(i);
			t++;
		}, 10);
	};
</script>

<div class="container mx-auto w-full px-4 mb-4 flex flex-wrap lg:flex-nowrap">
	<div class="hidden lg:block w-[240px] lg:mr-10" />
	<div class="w-[240px] lg:absolute space-y-5 left-20 mx-auto lg:mr-10 mb-10">
		<h1 class="text-3xl font-bold text-center mx-auto">{display || username}</h1>

		<div class="text-secondary mx-auto text-center lg:text-left lg:mx-0">
			{subject.address && subject.address !== 'null' ? subject.address : ''}
		</div>

		<div class="flex justify-around">
			<a href={`/${subject.pubkey}/follows`}
				><b>{subject.follows.length}</b>
				<span class="text-secondary">{$t('user.following')}</span></a
			>
			<a href={`/${subject.pubkey}/followers`}
				><b>{subject.followers.length}</b>
				<span class="text-secondary">{$t('user.followers')}</span></a
			>
		</div>

		<div class="flex flex-wrap gap-2 w-full">
			{#if user && user.username !== subject.username && subject.pubkey}
				{#if following}
					<div class="w-full flex">
						<button
							class="rounded-full border py-3 px-6 font-bold hover:opacity-80 flex w-60 bg-black text-white"
							on:click={unfollow}
						>
							<div class="mx-auto flex">
								<Icon icon={'profile'} style="my-auto h-6 mr-2 invert" />
								<div class="my-auto mt-1">{$t('user.following')}</div>
							</div>
						</button>
					</div>
				{:else}
					<div class="w-full flex">
						<button
							class="rounded-full border py-3 px-6 font-bold hover:opacity-80 flex w-60"
							on:click={follow}
						>
							<div class="mx-auto flex">
								<Icon icon={'profile'} style="my-auto h-6 mr-2" />
								<div class="my-auto mt-1">{$t('user.follow')}</div>
							</div>
						</button>
					</div>
				{/if}

				<div class="w-full flex">
					<a href={`/${subject.pubkey}/messages`} class="mx-auto">
						<button class="rounded-full border py-3 px-6 font-bold hover:opacity-80 flex w-60">
							<div class="mx-auto flex">
								<Icon icon="support" style="mr-2 my-auto" />
								<div class="mt-1 my-auto">{$t('user.message')}</div>
							</div>
						</button>
					</a>
				</div>
			{/if}
			{#if !subject.anon && subject.username !== user?.username}
				<div class="w-full flex">
					<a href={user ? `/send/${subject.username}` : `/${subject.username}/receive`} class="mx-auto">
						<button class="rounded-full border py-3 px-6 font-bold hover:opacity-80 flex w-60">
							<div class="mx-auto flex">
								<Icon icon="send" style="mr-2" />
								<div class="mt-1">{$t('user.pay')}</div>
							</div>
						</button>
					</a>
				</div>
			{/if}

			{#if user?.id === subject.id}
				<div class="w-full flex">
					<a href={`/send`} class="mx-auto">
						<button class="rounded-full border py-3 px-6 font-bold hover:opacity-80 flex w-60">
							<div class="mx-auto flex">
								<Icon icon="send" style="mr-2" />
								<div class="mt-1">{$t('user.dashboard.send')}</div>
							</div>
						</button>
					</a>
				</div>
				<div class="w-full flex">
					<a href={`/${subject.username}/receive`} class="mx-auto">
						<button class="rounded-full border py-3 px-6 font-bold hover:opacity-80 flex w-60">
							<div class="mx-auto flex">
								<Icon icon="numpad" style="mr-2" />
								<div class="mt-1">{$t('user.dashboard.receive')}</div>
							</div>
						</button>
					</a>
				</div>
				<!-- <div class="w-full flex"> -->
				<!-- 	<a href={`/${subject.username}/receive`} class="mx-auto"> -->
				<!-- 		<button class="rounded-full border py-3 px-6 font-bold hover:opacity-80 flex w-60"> -->
				<!-- 			<div class="mx-auto flex"> -->
				<!-- 				<Icon icon="support" style="mr-2" /> -->
				<!-- 				<div class="mt-1">{$t('user.messages')}</div> -->
				<!-- 			</div> -->
				<!-- 		</button> -->
				<!-- 	</a> -->
				<!-- </div> -->
			{/if}
		</div>
	</div>

	<div class="w-full">
		<div class="mx-auto space-y-5 lg:max-w-lg xl:max-w-2xl">
			<slot />
		</div>
	</div>
</div>

{#if currency && $animatedRate}
	<div class="flex fixed w-full px-4 bg-white py-2 bottom-0 bg-opacity-90 tabular-nums">
		<div class="text-secondary flex mr-auto">
			<div class="flex mr-1">
				<div class="my-auto mr-1">1</div>
				<img src="/images/bitcoin.svg" class="w-5 my-auto" alt="Bitcoin" />
			</div>
			<div>= {f($animatedRate, currency)}</div>
		</div>
		<div class="text-secondary flex ml-auto">
			<div class="flex">
				<div class="mr-1">⚡️{s((1 * sats) / $animatedRate)} =</div>
				<div>{f(1, currency)}</div>
			</div>
		</div>
	</div>
{/if}